
from django.db import models
from django import forms

from .widgets import EditorMDWidget, EditorMDUploadingWidget


class EditorMDField(models.TextField):

    def __init__(self, *args, **kwargs):
        self.config_name = kwargs.pop("config_name", "default")
        self.extra_js_files = kwargs.pop("extra_js_files", [])
        self.include_jquery = kwargs.pop("include_jquery", True)
        self.image_upload_url = kwargs.pop("image_upload_url", None)

        super(EditorMDField, self).__init__(*args, **kwargs)


    def formfield(self, **kwargs):
        defaults = { 
            'form_class': self._get_form_class(),
            "config_name": self.config_name,
            "include_jquery": self.include_jquery,
            "image_upload_url": self.image_upload_url,
            "extra_js_files": self.extra_js_files,
        }
        defaults.update(kwargs)

        return super(EditorMDField, self).formfield(**defaults)


    @staticmethod
    def _get_form_class():
        return EditorMDFormField



class EditorMDFormField(forms.fields.CharField):

    def __init__(self, config_name="default", extra_js_files=(), 
            include_jquery=True, image_upload_url=None, *args, **kwargs):

        kwargs.update({ 'widget': EditorMDWidget(
            config_name=config_name, 
            image_upload_url=image_upload_url,
            include_jquery=include_jquery, 
            extra_js_files=extra_js_files,
        )})

        super(EditorMDFormField, self).__init__(*args, **kwargs)




class EditorMDUploadingField(EditorMDField):

    @staticmethod
    def _get_form_class():
        return EditorMDUploadingFormField


class EditorMDUploadingFormField(forms.fields.CharField):

    def __init__(self, config_name="default", extra_js_files=(), 
            include_jquery=True, image_upload_url=None, *args, **kwargs):

        kwargs.update({ 'widget': EditorMDUploadingWidget(
            config_name=config_name, 
            image_upload_url=image_upload_url,
            include_jquery=include_jquery, 
            extra_js_files=extra_js_files,
        )})

        super(EditorMDUploadingFormField, self).__init__(*args, **kwargs)
